<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
					<jk-add-button :permission-prefix="prefix" class="margin-5-0 item-gap" @click="onAddBtn"></jk-add-button>
				</div>
				<div class="flex-end-center-wrap">
					<jk-select v-model="searchParams.auditState" :option-list="auditStateList" placeholder="状态" clearable size="small" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
					<jk-select v-model="searchParams.processId" :option-list="processList" placeholder="工序" clearable size="small" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
					<jk-select v-model="searchParams.spinTypeId" :option-list="spinTypeList" placeholder="纺纱方式" clearable size="small" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
					<jk-search-input v-model="searchParams.code" placeholder="模版编码" size="small" class="search-bar-item-size2 margin-5-0 item-gap" clearable @on-enter="onSearchBtn" />
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>

		<jk-card>
			<jk-table :loading="tableLoading" :data="tableData" :height="tableHeight + 20">
				<vxe-column title="模版编码" align="left" field="code" :min-width="150" fixed="left" />
				<vxe-column title="工序" align="center" field="processName" :width="70" fixed="left" />
				<vxe-column title="机型" align="left" field="machineModelName" :width="100" />
				<vxe-column title="纺纱方式" align="left" field="spinTypeName" :min-width="100" />
				<vxe-column title="工艺项" align="left" field="dataType" :min-width="1800">
					<template slot-scope="scope">
						<div class="flex-start-center">
							<div
								v-for="(item, index) in scope.row.itemList"
								:key="index"
								style="padding: 0 2px;width: 150px;flex-shrink: 0;"
							>
								<el-tag class="width-100-percent">{{ index + 1 }}、{{ item.itemName }}</el-tag>
							</div>
						</div>
					</template>
				</vxe-column>
				<vxe-column title="创建人" align="left" field="creatorName" :width="90"></vxe-column>
				<vxe-column title="创建时间" align="left" field="createTime" :width="160"></vxe-column>
				<vxe-column title="修改人" align="left" field="updaterName" :width="90"></vxe-column>
				<vxe-column title="修改时间" align="left" field="updateTime" :width="160"></vxe-column>
				<vxe-column title="状态" align="center" field="auditState" :width="100">
					<template slot-scope="scope">
						<jk-common-status :status="scope.row.auditState"></jk-common-status>
					</template>
				</vxe-column>
				<vxe-column title="操作" align="center" fixed="right" :width="310">
					<template slot-scope="scope">
						<div class="flex-center">
							<jk-button size="mini" icon="el-icon-document-copy" class="item-gap" @click="onCopyBtn(scope.row)"></jk-button>
							<jk-operation-bar
								:scope="scope"
								api-prefix="spin/technology-model"
								:permission-prefix="prefix"
								@on-detail="onDetailBtn"
								@on-edit="onEditBtn"
								@on-refresh="getListRequest"
							></jk-operation-bar>
						</div>
					</template>
				</vxe-column>
			</jk-table>

			<!-- 分页组件 -->
			<footer ref="box2" class="flex-end-center margin-top-10">
				<jk-page
					:page-index="searchParams.pageNo"
					:page-sizes="searchParams.pageSizes"
					:page-size="searchParams.pageSize"
					:total="totalCount"
					@on-change="onPageChange"
				/>
			</footer>
		</jk-card>
		<detail-dialog
			:data-id="dataId"
			:dialog-state="detailDialogState"
			@on-visible-change="onVisibleChangeOfDetailDialog"
		></detail-dialog>
		<save-dialog
			:is-copy="isCopy"
			:data-id="dataId"
			:dialog-state="saveDialogState"
			@on-visible-change="onVisibleChange"
			@on-finish="onFinish"
		></save-dialog>
	</div>
</template>

<script>
    import saveDialog from './save-dialog';
    import detailDialog from './detail-dialog';
    import calHeight from '/src/mixins/cal-table-height';
    import { pageConfig } from '/src/utils/jk/jk-tool';
    import {
        spinTechnologyModelPage
    } from '/src/api/technology/item';
    import { auditStateList } from '../../../utils/jk/status-option-list';
    import { systemBusinessDictPage } from '../../../api/basicData/businessDict';

    export default {
        name: 'TechnologyTemplate',
        components: {
            saveDialog,
            detailDialog
        },
        mixins: [calHeight],
        data() {
            return {
                apiItem: {
                    'page': spinTechnologyModelPage
                },
                spinTypeList: [],
                processList: [],
                detailDialogState: false,
                prefix: 'spin:technologyModel',
                auditStateList: auditStateList,
                dataId: null,
                workshopList: [],
                saveDialogState: false,
                tableLoading: true,
                tableData: [],
                totalCount: 0,
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    code: '',
                    processId: null,
                    auditState: null
                },
                isCopy: false
            };
        },
        methods: {
            onCopyBtn(row) {
                this.isCopy = true;
                this.dataId = row.id;
                this.saveDialogState = true;
            },
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            onDetailBtn(e) {
                this.dataId = e.id;
                this.detailDialogState = true;
            },
            onVisibleChangeOfDetailDialog(e) {
                this.detailDialogState = e;
                if (!e) {
                    this.dataId = null;
                }
            },
            onFinish() {
                this.saveDialogState = false;
                this.getListRequest();
            },
            onVisibleChange(e) {
                this.saveDialogState = e;
                if (!e) {
                    this.dataId = null;
                }
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                return this.apiItem['page'](this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res;
                        this.totalCount = res.data.count;
                    }
                });
            },
            /** 新增按钮操作 */
            onAddBtn() {
                this.isCopy = false;
                this.saveDialogState = true;
            },
            /** 修改按钮操作 */
            onEditBtn(row) {
                this.isCopy = false;
                this.dataId = row.id;
                this.saveDialogState = true;
            },
            getSpinTypeListRequest() {
                return systemBusinessDictPage({ categoryCode: 'SPIN_TYPE' }).then(res => {
                    if (res.data.status === 200) {
                        this.spinTypeList = res.data.res || [];
                    }
                });
            },
            async getDependentData() {
                this.tableLoading = true;
                await this.getSpinTypeListRequest();
                await this.getListRequest();
                this.tableLoading = false;
            }
        },
        mounted() {
            this.processList = this.$store.state.user.userProcess.processList;
            if (!this.searchParams.processId) {
                // this.$set(this.searchParams, 'processId', this.$store.state.user.userProcess.defaultProcessId);
            }
            this.getDependentData();
        }
    };
</script>
